package cn.terminus.service.customer.job;

import cn.terminus.client.customer.contractinfo.param.RentContractNewParam;
import cn.terminus.client.customer.contractinfo.param.SellContractNewParam;
import cn.terminus.client.customer.custompurposeroom.param.CustomPurposeRoomNewParam;
import cn.terminus.client.customer.custompurposeroom.result.RoomDetailResult;
import cn.terminus.common.customer.constant.ContractTypeEnum;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * @author leslie.zhang
 * @date 2021/9/16 3:31 下午
 */
@Component
@Slf4j
public class RoomStatusHandler {

    @Resource
    KafkaTemplate<String, String> kafkaTemplate;

    @Value("${topicName_roomStatus}")
    String topicName_roomStatus;

    @Value("${topicKeyName_roomStatus}")
    String topicKeyName_roomStatus;

    public void sendMsgWithEffective(Integer type, Long cboRoomId) {
        RoomStatusModel roomStatusModel = new RoomStatusModel();
        roomStatusModel.setRoomId(cboRoomId);
        if (type.equals(ContractTypeEnum.rent.getCode())) {
            roomStatusModel.setStatus(4);
        } else {
            roomStatusModel.setStatus(3);
        }
        log.info("房屋合同生效：MQ param :{}", JSON.toJSONString(roomStatusModel));
        kafkaTemplate.send(topicName_roomStatus, topicKeyName_roomStatus, JSON.toJSONString(roomStatusModel));
    }

    public void sendMsgWithInvalid(Integer type, Long cboRoomId) {

        RoomStatusModel roomStatusModel = new RoomStatusModel();
        roomStatusModel.setRoomId(cboRoomId);
        if (type.equals(ContractTypeEnum.rent.getCode())) {
            roomStatusModel.setStatus(0);
        } else {
            roomStatusModel.setStatus(1);
        }
        log.info("房屋合同到期，房间状态修改：MQ param :{}", JSON.toJSONString(roomStatusModel));
        kafkaTemplate.send(topicName_roomStatus, topicKeyName_roomStatus, JSON.toJSONString(roomStatusModel));
    }

    public void sendBatchMsgWithEffective(List<CustomPurposeRoomNewParam> purposeRoomNewList, Integer type) {
        if (!CollectionUtils.isEmpty(purposeRoomNewList)) {
            purposeRoomNewList.forEach(r -> {
                sendMsgWithEffective(type, r.getCboRoomId());
            });
        }
    }

    public void sendMsgWithEditSellContract(List<RoomDetailResult> finalRoomDetailResults, SellContractNewParam param) {
        // 使旧的失效
        if (!CollectionUtils.isEmpty(finalRoomDetailResults)) {
            finalRoomDetailResults.forEach(r -> {
                sendMsgWithInvalid(ContractTypeEnum.sell.getCode(), r.getCboRoomId());
            });
        }
        List<CustomPurposeRoomNewParam> purposeRoomNewList = param.getPurposeRoomNewList();
        LocalDate effectiveBeginTime = param.getEffectiveBeginTime();
        LocalDate now = LocalDate.now();
        // 新加数据 有效如果已生效，使其生效
        if (!CollectionUtils.isEmpty(purposeRoomNewList)) {
            if (effectiveBeginTime.isBefore(now) || now.isEqual(effectiveBeginTime)) {
                purposeRoomNewList.forEach(r -> {
                    sendMsgWithEffective(ContractTypeEnum.sell.getCode(), r.getCboRoomId());
                });
            }
        }
    }

    public void sendMsgWithEditRentContract(List<RoomDetailResult> finalRoomDetailResults, RentContractNewParam param) {
        // 使旧的失效
        if (!CollectionUtils.isEmpty(finalRoomDetailResults)) {
            finalRoomDetailResults.forEach(r -> {
                sendMsgWithInvalid(ContractTypeEnum.rent.getCode(), r.getCboRoomId());
            });
        }
        List<CustomPurposeRoomNewParam> purposeRoomNewList = param.getPurposeRoomNewList();
        LocalDate effectiveBeginTime = param.getEffectiveBeginTime();
        LocalDate effectiveEndTime = param.getEffectiveEndTime();

        LocalDate now = LocalDate.now();
        if ((now.isAfter(effectiveBeginTime) && now.isBefore(effectiveEndTime))
                || now.isEqual(effectiveBeginTime) || now.isEqual(effectiveEndTime)) {
            // 新加数据 如果已生效，使其生效
            if (!CollectionUtils.isEmpty(purposeRoomNewList)) {
                    purposeRoomNewList.forEach(r -> {
                        sendMsgWithEffective(ContractTypeEnum.rent.getCode(), r.getCboRoomId());
                    });
            }
        }
    }
}
